<template>
  <div>
    <van-nav-bar title="个人信息" left-arrow @click-left="$router.back()" />

    <!-- 头像 -->
    <!-- document.qs('input).click() -->
    <input
      type="file"
      @change="onSlected"
      hidden
      ref="input"
      multiple
      accept=".png,.jpg,.jpeg"
    />
    <van-cell is-link title="头像" @click="$refs.input.click()">
      <van-image round width="0.8rem" height="0.8rem" :src="userInfo.photo" />
    </van-cell>
    <van-cell is-link title="昵称" @click="isShowNvname = true">
      <span>{{ userInfo.name }}</span>
    </van-cell>

    <van-cell is-link title="性别" @click="isShowGender = true">
      <span>{{ userInfo.gender === 0 ? '男' : '女' }}</span>
    </van-cell>

    <van-cell is-link title="生日" @click="isShowBirthday = true">
      <span>{{ userInfo.birthday }}</span>
    </van-cell>

    <!-- 弹出层 -->
    <!-- 更新头像 -->
    <van-popup
      v-model="isShowAvater"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-avatar 
      :photo="photo"
      @close="isShowAvater=false"
      v-if="isShowAvater"
      @update-photo="[userInfo.photo=$event,isShowAvater=false]"
      ></update-avatar>
    </van-popup>
    <!-- 更新昵称 -->
    <van-popup
      v-model="isShowNvname"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <updata-nickname
        @close="isShowNvname = false"
        :name.sync="userInfo.name"
        v-if="userInfo.name && isShowNvname"
      ></updata-nickname>
    </van-popup>
    <!-- 更新性别 -->
    <van-popup
      v-model="isShowGender"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <updata-gender
        v-if="userInfo.name && isShowGender"
        :gender.sync="userInfo.gender"
        @close="isShowGender = false"
      ></updata-gender>
    </van-popup>
    <!-- 生日 -->
    <van-popup
      v-model="isShowBirthday"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <update-birthday
        v-if="userInfo.birthday && isShowBirthday"
        :birthday.sync="userInfo.birthday"
        @close="isShowBirthday = false"
      ></update-birthday>
    </van-popup>
  </div>
</template>

<script>

import { getUserProfileAPI } from '@/api'
import UpdataNickname from './components/UpdataNickname.vue'
import UpdataGender from './components/UpdateGender.vue'
import UpdateBirthday from './components/UpdateBirthday.vue'
import UpdateAvatar from './components/UpdateAvatar.vue'
import { resolveToBase64 } from '@/utils'
export default {
  name: 'GetUserProfile',
  components: {
    UpdataNickname,
    UpdataGender,
    UpdateBirthday,
    UpdateAvatar
  },
  data() {
    return {
      userInfo: {},
      isShowNvname: false,
      isShowGender: false,
      isShowBirthday: false,
      isShowAvater: false,
      photo: ''
    }
  },
  created() {
    this.getUserProfile()
  },
  methods: {
    async getUserProfile() {
      const { data } = await getUserProfileAPI()
      this.userInfo = data.data
    },
    async onSlected() {
      //1.获取用户选择的文件(保存在input.files)
      const file = this.$refs.input.files[0]
      //2.将file对象转成base64
      const base64 = await resolveToBase64(file)
      this.photo = base64
      //3.打开弹层
      this.isShowAvater = true
      //4.手动将input.value属性清空
      this.$refs.input.value=""
    }
  }
}
</script>

<style></style>
